package cpa.prowon.gui;

import java.util.ResourceBundle;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import pho.prowon.category.VisibleIndexCategory;

import cpa.prowon.Application;
import cpa.prowon.gui.actions.CopyFileNamesToClipboardAction;
import cpa.prowon.gui.actions.DeleteAction;
import cpa.prowon.gui.actions.ImageInformationAction;
import cpa.prowon.gui.actions.OpenAction;
import cpa.prowon.gui.actions.RemoveAction;

/**
 * Represents the list viewer
 * @author Chris
 */
public class WindowListPart extends WindowPart {

	private static TableColumn sortedBy = null;
	private static boolean ascending = false;
	private TableViewer tableViewer;
	private OpenAction actionOpen;
	private RemoveAction actionRemove;
	private CopyFileNamesToClipboardAction actionCopy;
	private ImageInformationAction actionImageInformation;
	private DeleteAction actionDelete;
	
	/**
	 * Handles the sorting
	 * @author Chris
	 */
	private class SortSelectionListener implements SelectionListener
	{
		String sortTag = "CaptionAbstract";

		private SortSelectionListener (String tag)
		{
			sortTag = tag;
		}

		public void widgetSelected(SelectionEvent e)
		{
			TableColumn tc = ((TableColumn) e.widget);
			VisibleIndexCategory vic = ((VisibleIndexCategory) tableViewer.getInput());

			if (sortedBy != null)
			{
				sortedBy.setText(sortedBy.getText().substring(0,sortedBy.getText().length()-2));
			}

			if (tc == sortedBy)
			{
				ascending = !ascending;
			}
			else
			{
				ascending = true;
				sortedBy = tc;
			}

			vic.resort(sortTag, ascending);
			tc.setText(tc.getText()+ (ascending ? " v" : " ^"));

			tableViewer.refresh();
			
		}

		public void widgetDefaultSelected(SelectionEvent e)
		{
		}

	}

	/**
	 * This class handles key strokes
	 * @author Chris
	 */
	private class TableViewKeyAdapter extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			if (e.character == SWT.DEL)
			{
				actionRemove.run();
			}
			if (e.character == SWT.LF)
			{
				actionImageInformation.run();
			}
		}
	}
	
	/**
	 * This class handles double clicks
	 * @author Chris
	 */
	private class TableViewDoubleClickListener implements IDoubleClickListener
	{
		public void doubleClick(DoubleClickEvent event)
		{
			actionImageInformation.run();
		}
	}

	/**
	 * Constructor
	 * @param aMainWindow
	 * @author Chris
	 */
	public WindowListPart(MainWindow aMainWindow) {
		super(aMainWindow);
	}

	/* (non-Javadoc)
	 * @see cpa.prowon.gui.WindowPart#getTitle()
	 */
	protected String getTitle() {
		return Application.getInstance().getResourceBundle().getString("title_picture_list");
	}

	/* (non-Javadoc)
	 * @see cpa.prowon.gui.WindowPart#getIcon()
	 */
	protected String getIcon() {
		return "icons/picture.gif";
	}

	/**
	 * Create the actions for this window part
	 * @author Chris
	 */
	protected void createActions()
	{
		actionOpen = new OpenAction(getMainWindow());
		actionRemove = new RemoveAction(getMainWindow());
		actionCopy = new CopyFileNamesToClipboardAction(getMainWindow());
		actionImageInformation = new ImageInformationAction(getMainWindow());
		actionDelete = new DeleteAction(getMainWindow());
	}

	/* (non-Javadoc)
	 * @see cpa.prowon.gui.WindowPart#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected void createContents(Composite parent) {

		// Get the resource bundle	
		ResourceBundle rb = Application.getInstance().getResourceBundle();

		// Create the picture list viewer
		tableViewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI);
		Table table = tableViewer.getTable();
		
		// Create the table columns
		TableColumn col1 = new TableColumn(table, SWT.LEFT);
		col1.setWidth(100);
		col1.setText(rb.getString("imageinfo_title"));
		col1.addSelectionListener(new SortSelectionListener("CaptionAbstract"));
		TableColumn col2 = new TableColumn (table,SWT.LEFT);
		col2.setWidth(100);
		col2.setText(rb.getString("imageinfo_category"));
		col2.addSelectionListener(new SortSelectionListener("Category"));
		TableColumn col3 = new TableColumn (table,SWT.RIGHT);
		col3.setWidth(100);
		col3.setText(rb.getString("imageinfo_date"));
		col3.addSelectionListener(new SortSelectionListener("DateCreated"));
		table.setHeaderVisible(true);

		// Set the content and label provider as well as the filter
		tableViewer.setContentProvider(new PictureListContentProvider());
		tableViewer.setLabelProvider(new PictureListLabelProvider());
	
		// Add a double click listener to the table view
		tableViewer.addDoubleClickListener(new TableViewDoubleClickListener());

		// Register the key listeners
		tableViewer.getTable().addKeyListener(new TableViewKeyAdapter());

		// Set the content and label provider as well as the filter
		tableViewer.setContentProvider(new PictureListContentProvider());
		tableViewer.setLabelProvider(new PictureListLabelProvider());

		
		// Add the context menu to the picture list viewer
		MenuManager menu_manager = new MenuManager();
		tableViewer.getTable().setMenu(menu_manager.createContextMenu(tableViewer.getTable()));
		menu_manager.add(actionImageInformation);
		menu_manager.add(new Separator());
		menu_manager.add(actionRemove);
		menu_manager.add(actionDelete);
		menu_manager.add(new Separator());
		menu_manager.add(actionCopy);
		menu_manager.add(new Separator());
		menu_manager.add(actionOpen);
		
		// Register the appropriate actions to the picture list detail viewer
		tableViewer.addSelectionChangedListener(actionImageInformation);
		tableViewer.addSelectionChangedListener(actionRemove);
		tableViewer.addSelectionChangedListener(actionDelete);
		tableViewer.addSelectionChangedListener(actionOpen);
		tableViewer.addSelectionChangedListener(actionCopy);
			
	}

	/**
	 * Return the table viewer
	 * @return The table viewer widget
	 * @author Chris
	 */
	public TableViewer getTableViewer()
	{
		return tableViewer;
	}
	
	/**
	 * Return the selection from the picture list
	 * @return The selected object of the picture list viewer
	 * @author Chris
	 */
	public IStructuredSelection getSelection()
	{
		return (IStructuredSelection) (getTableViewer().getSelection());
	}
	
	/**
	 * Register the edit menu actions
	 * @param MenuManager The menu manager to put the actions in
	 * @author Chris
	 */
	public void registerEditMenuActions(MenuManager menuMgr)
	{
		menuMgr.add(new Separator());
		menuMgr.add(actionImageInformation);
		menuMgr.add(new Separator());
		menuMgr.add(actionRemove);
		menuMgr.add(actionDelete);
		menuMgr.add(new Separator());
		menuMgr.add(actionCopy);
		menuMgr.add(new Separator());
		menuMgr.add(actionOpen);
	}

	/**
	 * Register the toolbar actions
	 * @param ToolBarManager The tool bar manager to put the actions in
	 * @author Chris
	 */
	public void registerToolBarActions(ToolBarManager toolBarMgr)
	{
		toolBarMgr.add(new Separator());
		toolBarMgr.add(actionRemove);
		toolBarMgr.add(actionDelete);
		toolBarMgr.add(new Separator());
		toolBarMgr.add(actionCopy);
		toolBarMgr.add(new Separator());
		toolBarMgr.add(actionOpen);
	}

}
